package com.zzuli.controller;

import com.zzuli.entity.AreaInfoEntity;
import com.zzuli.entity.Result;
import com.zzuli.service.AreaInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/area")
public class AreaInfoController {

    @Autowired
    private AreaInfoService areaInfoService;

    @GetMapping("/selectByPage")
    public Result selectByPage(Integer page, Integer limit, AreaInfoEntity areaInfoEntity) {
        Map<String, Object> data = areaInfoService.selectAll(page,limit,areaInfoEntity);
        Result result = new Result();
        result.setCode(200);
        result.setMsg("查询成功");
        result.setData(data);
        return result;
    }

    @PostMapping("/addArea")
    public Result addArea(AreaInfoEntity areaInfoEntity){
        Result result = new Result();
        boolean flag = areaInfoService.addArea(areaInfoEntity);
        if (flag){
            result.setCode(200);
            result.setMsg("区域添加成功");
        }else {
            result.setCode(500);
            result.setMsg("区域添加失败");
        }
        return result;
    }

    @PostMapping("/deleteArea")
    public Result deleteArea(HttpServletRequest request){
        Result result = new Result();
        String[] areaIds = request.getParameterValues("allAreaId[]");
        System.out.println(areaIds);
        boolean flag = areaInfoService.deleteArea(areaIds);
        if (flag){
            result.setCode(200);
            result.setMsg("区域删除成功");
        }else {
            result.setMsg("区域删除失败");
        }
        return result;
    }

    @PostMapping("/updateArea")
    public Result updateArea(AreaInfoEntity areaInfoEntity){
        Result result = new Result();
        boolean flag = areaInfoService.updateArea(areaInfoEntity);
        if (flag) {
            result.setCode(200);
            result.setMsg("区域修改成功");
        } else {
            result.setCode(500);
            result.setMsg("区域修改失败");
        }
        return result;
    }

    @GetMapping("/selectBuildCount")
    public Result selectBuildCount(){
        Result result = new Result();
        List<Integer> list = areaInfoService.selectBuildCount();
        if (list != null){
            result.setCode(200);
            result.setMsg("楼号数量查询成功");
            System.out.println(list);
            result.setData(list);
        }else {
            result.setCode(500);
            result.setMsg("楼号数量查询失败");
        }
        return result;
    }
}
